package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.model.article.ApAuthor;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.MediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.RealNameMapper;
import com.usian.user.service.RealNameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class RealNameServiceImpl extends BaseServiceImpl<RealNameMapper, ApUserRealname> implements RealNameService {
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private MediaFeign mediaFeign;
    @Autowired
    private ArticleFeign articleFeign;

    @Override
    public ResponseResult getRealList(AuthDto dto) {baseMapper.selectList(null);
        if (dto == null) return paramError;
        return getPageData(dto, Wrappers.<ApUserRealname>lambdaQuery().eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus()));
    }

    /**
     * 用户提交认证信息后，后台管理平台进行认证审核
     * @param dto
     * @return
     */
    @Override
    @GlobalTransactional
//    @Transactional
    public ResponseResult authentication(AuthDto dto) {
        if (dto == null || dto.getId() == null || dto.getStatus() == null || (dto.getStatus() != 2 && dto.getStatus() != 9))
            return paramError;
        //更新认证表的认证状态
        update(Wrappers.<ApUserRealname>lambdaUpdate().eq(ApUserRealname::getId, dto.getId()).set(ApUserRealname::getStatus, dto.getStatus()).set(StringUtils.isNotEmpty(dto.getMsg()),ApUserRealname::getReason,dto.getMsg()));
        //看当前是通过还是驳回
        if (dto.getStatus() == 9) {
            //通过认证id，查询认证信息
            ApUserRealname realName = getById(dto.getId());
            ApUser apUser = apUserMapper.selectById(realName.getUserId());
            if (apUser == null) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            //如果认证通过，将用户表的用户角色改为1
            apUser.setFlag((short) 1);
            apUserMapper.updateById(apUser);
            //向自媒体人表中添加自媒体人信息
            WmUser wmUser = new WmUser();
            BeanUtils.copyProperties(apUser,wmUser);
            wmUser.setApUserId(apUser.getId());
            wmUser.setNickname(apUser.getName());
            wmUser.setStatus(9);
            ResponseResult mediaResult = mediaFeign.save(wmUser);
            wmUser.setId((Integer) mediaResult.getData());
            //向文章模块保存作者信息
            ApAuthor author = new ApAuthor();
            author.setName(apUser.getName());
            author.setType(2);
            author.setUserId(apUser.getId());
            author.setCreatedTime(new Date());
            author.setWmUserId(wmUser.getId());
            ResponseResult authorResult = articleFeign.saveAuthor(author);
            Integer authorId = (Integer) authorResult.getData();
            //更新自媒体人表中的作者id
            wmUser.setApAuthorId(authorId);
            ResponseResult result = mediaFeign.updateAuthorId(wmUser);
            System.out.println(result.getData());
        }
        return success;
    }
}
